package com.lxlg.lxlg_order.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lxlg.core.utils.Result;
import com.lxlg.lxlg_order.client.param.SearchDayStatementFOrm;
import com.lxlg.lxlg_order.client.result.StatementsInfo;
import com.lxlg.lxlg_order.common.OrderException.OrderException;
import com.lxlg.lxlg_order.common.OrderException.OrderExceptionCode;
import com.lxlg.lxlg_order.common.constvalue.AllOrderConstValue;
import com.lxlg.lxlg_order.infra.mapper.DayStatementMapper;
import com.lxlg.lxlg_order.infra.mapper.GoodsOrderMapper;
import com.lxlg.lxlg_order.infra.mapper.SeckillOrderMapper;
import com.lxlg.lxlg_order.infra.model.DayStatement;
import com.lxlg.lxlg_order.infra.model.parm.DayStatementInfo;
import com.lxlg.lxlg_order.infra.model.parm.SkuIdAndMoney;
import com.lxlg.lxlg_order.service.service.DayStatementService;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author kevin
 * @since 2022-04-28
 */
@Service
public class DayStatementServiceImpl extends ServiceImpl<DayStatementMapper, DayStatement> implements DayStatementService {
    @Resource
    private GoodsOrderMapper goodsOrderMapper;
    @Resource
    private SeckillOrderMapper seckillOrderMapper;

//    SELECT oag.sku_id ,sum(oag.goods_pay_allprice),oag.merchant_id,oag.merchant_name from goods_order go
//    LEFT JOIN order_and_goods oag
//    on go.order_id=oag.order_id
//    WHERE go.order_state=5 and oag.goods_state=1
//    AND go.sale_end_time <"2022-05-07 12:06:59"
//    GROUP BY oag.sku_id,oag.merchant_id,oag.merchant_name

    @Scheduled(cron = "0 0 0 * * ? ")//每天24点执行
    public void collectDayStatement(){
        //获取普通订单
        Date date=new   Date(); //取时间
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(calendar.DATE,-1); //把日期往后增加一天,整数  往后推,负数往前移动
        Date date0=calendar.getTime(); //这个时间就是日期往后推一天的结果

        SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
        System.out.println("今天的日期："+df.format(date));
        Date dateSave = new Date(date.getTime() - 1 * 60 * 60 * 1000);


        List<SkuIdAndMoney> salesamount = goodsOrderMapper.collectDayStatement(AllOrderConstValue.order_payState_complate,AllOrderConstValue.order_goodsState_pay,date0,date);

        //获取秒杀订单
        List<SkuIdAndMoney> salesamount2= seckillOrderMapper.collectDayStatement(AllOrderConstValue.order_payState_complate,date0,date);
        HashMap<Integer,SkuIdAndMoney> hmp= new HashMap<Integer,SkuIdAndMoney>();
        List<SkuIdAndMoney> salesamount00=new ArrayList<SkuIdAndMoney>();
        for (SkuIdAndMoney skuIdAndMoney : salesamount) {
            SkuIdAndMoney skuIdAndMoney1 = hmp.get(skuIdAndMoney.getSkuId());
            if(null==skuIdAndMoney1){
                hmp.put(skuIdAndMoney.getSkuId(),skuIdAndMoney);
                salesamount00.add(skuIdAndMoney);
            }else {
                SkuIdAndMoney skuIdAndMoney2 = new SkuIdAndMoney();
                skuIdAndMoney2.setSkuId(skuIdAndMoney.getSkuId());
                String s = new BigDecimal(skuIdAndMoney.getAllMoney()).add(new BigDecimal(skuIdAndMoney1.getAllMoney())).toString();
                skuIdAndMoney2.setAllMoney(s);
                skuIdAndMoney2.setMerchantId(skuIdAndMoney.getSkuId());
                skuIdAndMoney2.setMerchantName(skuIdAndMoney.getMerchantName());
                skuIdAndMoney2.setSkuIdNum(skuIdAndMoney.getSkuIdNum()+skuIdAndMoney1.getSkuIdNum());
                salesamount00.add(skuIdAndMoney2);
            }
        }
        for (SkuIdAndMoney skuIdAndMoney : salesamount2) {
            SkuIdAndMoney skuIdAndMoney1 = hmp.get(skuIdAndMoney.getSkuId());
            if(null==skuIdAndMoney1){
                hmp.put(skuIdAndMoney.getSkuId(),skuIdAndMoney);
                salesamount00.add(skuIdAndMoney);
            }else {
                SkuIdAndMoney skuIdAndMoney2 = new SkuIdAndMoney();
                skuIdAndMoney2.setSkuId(skuIdAndMoney.getSkuId());
                String s = new BigDecimal(skuIdAndMoney.getAllMoney()).add(new BigDecimal(skuIdAndMoney1.getAllMoney())).toString();
                skuIdAndMoney2.setAllMoney(s);
                skuIdAndMoney2.setMerchantId(skuIdAndMoney.getSkuId());
                skuIdAndMoney2.setMerchantName(skuIdAndMoney.getMerchantName());
                skuIdAndMoney2.setSkuIdNum(skuIdAndMoney.getSkuIdNum()+skuIdAndMoney1.getSkuIdNum());
                salesamount00.add(skuIdAndMoney2);
            }
        }
        List<DayStatement> dayStatements = new ArrayList<>();
        for (SkuIdAndMoney skuIdAndMoney : salesamount00) {
            DayStatement dayStatement = new DayStatement();
            dayStatement.setSkuId(skuIdAndMoney.getSkuId());
            dayStatement.setDayStatementMerchantId(skuIdAndMoney.getMerchantId());
            dayStatement.setDayStatementPrice(skuIdAndMoney.getAllMoney());
            dayStatement.setDayStatementTime(dateSave);
            dayStatement.setDayStatementSalenum(skuIdAndMoney.getSkuIdNum());
        }
        boolean b = saveBatch(dayStatements);
        if(!b){
            throw new OrderException(OrderExceptionCode.DAYSTATEMENT_SAVE_ERROR);
        }
    }

    @Override
    public Result<List<StatementsInfo>> queryDayStatement(SearchDayStatementFOrm searchDayStatementFOrm) {
        List<DayStatementInfo> dayStatementsInfo = baseMapper.getDayStatement(searchDayStatementFOrm.getStartTime(),searchDayStatementFOrm.getEndtTime());
        List<StatementsInfo> statementsInfos = new ArrayList<>();
        for (DayStatementInfo dayStatementInfo : dayStatementsInfo) {
            StatementsInfo statementsInfo = new StatementsInfo();
            statementsInfo.setSkuId(dayStatementInfo.getSkuId());
            statementsInfo.setStatementPrice(dayStatementInfo.getDayStatementPrice());
            statementsInfo.setStatementSalenum(dayStatementInfo.getDayStatementSalenum());
            statementsInfo.setStatementGoodsName(dayStatementInfo.getDayStatementGoodsName());
            statementsInfo.setStatementMerchantId(dayStatementInfo.getDayStatementMerchantId());
            statementsInfo.setStatementTime(dayStatementInfo.getDayStatementTime());
            statementsInfos.add(statementsInfo);
        }
        return Result.getInstanceList(StatementsInfo.class).setData(statementsInfos);

    }




}
